package com.wang.leetcode;

/**
 * @Author: along
 * @Create: 2021/4/14 13:30
 * @Description:链表的倒数第k个节点，返回链表
 */
public class Sword22 {
    class ListNode {
       int val;
       ListNode next;
       ListNode(int x) { val = x; }
    }
    public ListNode getKthFromEnd(ListNode head, int k) {
        //定义快慢指针
        ListNode fast = head;
        ListNode slow = head;
        //快指针先走k步
        while(k >0) {
            fast = fast.next;
            k--;
        }
        //快慢指针一起走 当快指针为null，
        while(fast != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }
}
